package com.zhongxi.guoyuan.fragment.RewardCoin;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.rewardCoin.RewardCoinDetailAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.rewardcoin.MallInfoResult;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinDetailResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class RewardCoinDetailFragment extends BaseFragment {

    private NoScrollGridView gvList;
    private RewardCoinDetailAdapter gvAdapter;
    private WebView wv;
    private String type;
    private TextView tvAddress;
    private TextView tvPhone;
    private ImageView ivCall;
    private MallInfoResult result;
    private List<RewardCoinDetailResult.RewardRecommend> recommendInfo;

    public RewardCoinDetailFragment() {
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_reward_coin_detail, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        wv = (WebView) getView().findViewById(R.id.wv);
        tvPhone = (TextView) getView().findViewById(R.id.tvPhone);
        tvAddress = (TextView) getView().findViewById(R.id.tvAddress);
        ivCall = (ImageView) getView().findViewById(R.id.ivCall);
        gvList = (NoScrollGridView) getView().findViewById(R.id.gvList);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        //同款推荐点击进入详情页
        gvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, RewardCoinDetailActivity.class);
                intent.putExtra("id", recommendInfo.get(position).getId());
                startActivity(intent);
                getActivity().finish();
            }
        });
        //打电话
        ivCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (null != result && !"".equals(result))
                        if (null != result.getData() && !"".equals(result.getData()))
                            if (null != result.getData().getMphone() && !"".equals(result.getData().getMphone())) {
                                Intent intent = new Intent(Intent.ACTION_DIAL);
                                Uri data = Uri.parse("tel:" + result.getData().getMphone());
                                intent.setData(data);
                                startActivity(intent);
                            }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    public void initProjectData(RewardCoinDetailResult result) {
        try{
            if(null!=result.getGoodsInfo()&&!"".equals(result.getGoodsInfo())){
                RewardCoinDetailResult.RewardCoinDetailData goodsInfo = result.getGoodsInfo();
                if(null!=goodsInfo.getDesc()&&!"".equals(goodsInfo.getDesc())){
                    wv.loadDataWithBaseURL(null, getNewContent(goodsInfo.getDesc()), "text/html", "utf-8", null);
                }
            }
            if(null!=result.getRecommendInfo()&&!"".equals(result.getRecommendInfo())){
                recommendInfo = result.getRecommendInfo();
                if(null!=recommendInfo&&recommendInfo.size()>0){
                    gvAdapter = new RewardCoinDetailAdapter(context, recommendInfo);
                    gvList.setAdapter(gvAdapter);
                }
            }
            //获取店铺配置信息
            getShopInfo();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initInfo(MallInfoResult result) {
        if(null!=result.getData()&&!"".equals(result.getData())){
            MallInfoResult.MallInfo info = result.getData();
            if(null!=info.getMphone()&&!"".equals(info.getMphone())){
                tvPhone.setText(info.getMphone());
            }
            if(null!=info.getAddress()&&!"".equals(info.getAddress())){
                tvAddress.setText(info.getAddress());
            }
        }
    }

    private String getNewContent(String htmltext){
        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }
        return doc.toString();
    }

    private void getShopInfo() {
        type = "info";
        getServer(Url.MALL_INFO, null, "", false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("info".equals(type)){
                result = gson.fromJson(response, MallInfoResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                initInfo(result);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
